package com.boot.admin.modules.sys.ai.config;

import com.boot.admin.modules.sys.ai.service.RealtimeWebSocketService;
import com.boot.admin.modules.sys.ai.service.StreamingWebSocketService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置 - 支持实时语音流传输和音频流传输
 *
 * @author 李飞洋
 * @date 2025/9/24
 */
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final StreamingWebSocketService streamingWebSocketService;
    private final RealtimeWebSocketService realtimeWebSocketService;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

        // 实时语音对话WebSocket端点 - 新增
        registry.addHandler(realtimeWebSocketService, "/ai/realtime/voice")
                .setAllowedOrigins("*") // 生产环境应该限制具体域名
                .withSockJS(); // 支持SockJS作为WebSocket的fallback

        // 实时语音对话纯WebSocket端点 - 新增
        registry.addHandler(realtimeWebSocketService, "/ai/realtime/ws")
                .setAllowedOrigins("*");

        // 原有的音频流WebSocket端点（用于非实时场景）
        registry.addHandler(streamingWebSocketService, "/ai/audio/stream")
                .setAllowedOrigins("*")
                .withSockJS();

        // 原有的纯WebSocket端点
        registry.addHandler(streamingWebSocketService, "/ai/audio/ws")
                .setAllowedOrigins("*");
    }
}